#!/bin/sh

# Slackware build script for oracle-xe

# Copyright 2015 Dhaby Xiloj <slack.dhabyx@gmail.com>
# All rights reserved.
#
# Some ideas has taked from
# https://github.com/aclemons/slackbuilds/tree/master/system/oracle-xe-11g
#   Andrew Clemons, Wellingtong, New Zeland
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PRGNAM=oracle-xe
VERSION=${VERSION:-11.2.0}
RELEASE=${RELEASE:-1.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    x86_64) ARCH=x86_64 ;;
    *) echo "The package for $(uname -m) architecture is not available."; exit 1;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

RPMNAME="${PRGNAM}-${VERSION}-${RELEASE}.x86_64.rpm"
ZIPNAME="${RPMNAME}.zip"

set -e

# Check if oracle group and user exist
if [ "$(grep ^oracle /etc/passwd)" = "" -o "$(grep ^dba /etc/group)" = "" ] ; then
    cat << EOF
You must have apropiate user and group for oracle-xe.
    Example:
        # groupadd -g 329 dba
        # useradd -d /u01/app/oracle -s /bin/bash -u 329 -g 329 oracle
EOF
    exit 1
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rvf $PRGNAM-$VERSION

mkdir $PRGNAM-$VERSION
cd $PRGNAM-$VERSION
unzip $CWD/$ZIPNAME
cd Disk1
rpm2cpio < $RPMNAME | cpio -imdv

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# moving everything to proper place
mv u01 usr $PKG
mkdir -p $PKG/etc/rc.d
mv etc/init.d/oracle-xe $PKG/etc/rc.d/rc.oracle-xe
chmod 644 $PKG/etc/rc.d/rc.oracle-xe
sed -i 's#/etc/init.d/oracle-xe#/etc/rc.d/rc.oracle-xe#g' $PKG/etc/rc.d/rc.oracle-xe

mkdir -p $PKG/etc/sysctl.d/
cp $CWD/10-oracle.xe.conf.new $PKG/etc/sysctl.d/

# copy environment files
mkdir -p $PKG/etc/profile.d
cp $PKG/u01/app/oracle/product/${VERSION}/xe/bin/oracle_env.sh \
    $PKG/etc/profile.d/oracle-env.sh
    echo "export TNS_ADMIN=\$ORACLE_HOME/network/admin" >> $PKG/etc/profile.d/oracle-env.sh

cp $PKG/u01/app/oracle/product/${VERSION}/xe/bin/oracle_env.csh \
    $PKG/etc/profile.d/oracle-env.csh
    echo "setenv TNS_ADMIN \$ORACLE_HOME/network/admin" >> $PKG/etc/profile.d/oracle-env.csh

# add oracle jdbc driver to classpath
mkdir -p $PKG/etc/profile.d/
cat << EOF > $PKG/etc/profile.d/oracle-jdbc-env.csh
#!/bin/csh
setenv CLASSPATH \$CLASSPATH:\$ORACLE_HOME/jdbc/lib/ojdbc6.jar
EOF

cat << EOF > $PKG/etc/profile.d/oracle-jdbc-env.sh
#!/bin/sh
export CLASSPATH=\$CLASSPATH:\$ORACLE_HOME/jdbc/lib/ojdbc6.jar
EOF

chmod 755 $PKG/etc/profile.d/*


# Remove -lipgo and -lsvml from sysliblist as they were not shipped in 11.2XE
sed -i "s/-lipgo //" $PKG/u01/app/oracle/product/11.2.0/xe/lib/sysliblist
sed -i "s/ -lsvml//" $PKG/u01/app/oracle/product/11.2.0/xe/lib/sysliblist

# fix bad konsole param
sed -i "s/konsole -T /konsole -p tabtitle=/" \
    $PKG/u01/app/oracle/product/11.2.0/xe/config/scripts/runsqlplus.sh

# creating empty dirs for proper oracle configuration
mkdir -p $PKG/u01/app/oracle/product/${VERSION}/xe/config/log
mkdir -p $PKG/u01/app/oracle/product/${VERSION}/xe/rdbms/{audiot,log}
mkdir -p $PKG/u01/app/oracle/product/${VERSION}/xe/network/{trace,log}

# Make symbolic links for some libs
(
  cd $PKG/u01/app/oracle/product/${VERSION}/xe/lib
  ln -s libagtsh.so.1.0 libagtsh.so
  ln -s libclntsh.so.11.1 libclntsh.so
  ln -s libocci.so.11.1 libocci.so
)

# set appropriate permissions for oracle
chown -R oracle:dba $PKG/u01
chmod 755 $PKG/u01/app/oracle/product/${VERSION}/xe/config/scripts/*.sh

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
mv $PKG/usr/share/doc/oracle_xe/* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
